package com.lancoo.znbkxx.teacher.aibkGenerateTestPaper.aibkGenerateDialog

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import com.lancoo.znbkxx.teacher.R
import com.lancoo.znbkxx.teacher.aibkTestCoach.kBase.KBaseRecyclerAdapter
import com.lancoo.znbkxx.teacher.aibkTestCoach.kBase.KBaseRecyclerViewHolder
import com.lancoo.znbkxx.teacher.aibkTestCoach.networkRequest.entity.TempletSelectedTypeListInfor
import kotlinx.android.synthetic.main.aibk_adapter_item_questype_rank.view.*

class AibkTempletQuesTypeAdapter(
        val activity: AppCompatActivity,
        val templetSelectedTypeListInfor: TempletSelectedTypeListInfor
) : KBaseRecyclerAdapter() {

    override fun getHolderView(parent: ViewGroup?, position: Int): View? {
        return LayoutInflater.from(activity)
                .inflate(R.layout.aibk_adapter_item_questype_rank, parent, false)
    }

    override fun onBindViewHolder(holder: KBaseRecyclerViewHolder, position: Int) {

        var genreName = templetSelectedTypeListInfor[position].GenreName
        val typeName = templetSelectedTypeListInfor[position].TypeName
        if (genreName.isEmpty()) {
            genreName = typeName
        }
        val isChecked = templetSelectedTypeListInfor[position].IsChecked

        holder.convertView.TvQuesTypeName.text = genreName
        holder.convertView.TvQuesTypeName.isSelected = isChecked
        holder.convertView.TvQuesTypeName.setOnClickListener {
            holder.convertView.TvQuesTypeName.isSelected = !holder.convertView.TvQuesTypeName.isSelected
            for(i in 0 until templetSelectedTypeListInfor.size){
                templetSelectedTypeListInfor[i].IsChecked = i == position
            }
            notifyDataSetChanged()
            clickQuesTypeListener!!.onClickQuesType(position)
        }

    }

    override fun getItemCount(): Int {
        return templetSelectedTypeListInfor.size
    }

    override fun getItemViewType(position: Int): Int {
        return position
    }

    override fun getItemId(position: Int): Long {
        return position.toLong()
    }

    fun perClickWhichButton(pos:Int){
        for(i in 0 until templetSelectedTypeListInfor.size){
            templetSelectedTypeListInfor[i].IsChecked = i == pos
        }
        notifyDataSetChanged()
    }

    private var clickQuesTypeListener: OnClickQuesTypeListener? = null

    /*
   * 设置点击事件
   * */
    interface OnClickQuesTypeListener {
        fun onClickQuesType(pos:Int)
    }

    fun setClickDialogListener(clickQuesTypeListener: OnClickQuesTypeListener?) {
        this.clickQuesTypeListener = clickQuesTypeListener
    }

}